#
# @lc app=leetcode.cn id=1679 lang=python3
#
# [1679] K 和数对的最大数目
#

# @lc code=start
from collections import defaultdict
from typing import List


class Solution:
    def maxOperations(self, nums: List[int], k: int) -> int:
        cnt = defaultdict(int)
        ans = 0
        for i,x in enumerate(nums):
            tag = k-x
            if tag in cnt:
                ans += 1
                cnt[tag] -= 1
                if cnt[tag] == 0:
                    del cnt[tag]
            else:
                cnt[x] += 1
        return ans
# @lc code=end

